package sjtu.ist.warehouse4ws.service;

import java.util.ArrayList;
import java.util.List;

import sjtu.ist.warehouse4ws.dao.CompanyDaoHibernate;
import sjtu.ist.warehouse4ws.dao.DepartmentDaoHibernate;
import sjtu.ist.warehouse4ws.dao.FileInfoDaoHibernate;
import sjtu.ist.warehouse4ws.dao.PrivilegeDaoHibernate;
import sjtu.ist.warehouse4ws.model.Company;
import sjtu.ist.warehouse4ws.model.Fileinfo;
import sjtu.ist.warehouse4ws.model.Privilege;
import sjtu.ist.warehouse4ws.tool.ListPage;
import sjtu.ist.warehouse4ws.vo.FullPrivilegeVO;

public class PrivilegeServiceImpl implements PrivilegeService{

	private PrivilegeDaoHibernate privilegeDao;
	private FileInfoDaoHibernate fileInfoDao;
	private CompanyDaoHibernate companyDao;
	private DepartmentDaoHibernate departmentDao;
	
	public void setPrivilegeDao(PrivilegeDaoHibernate privilegeDao){
		this.privilegeDao = privilegeDao;
	}
	
	public void setFileInfoDao(FileInfoDaoHibernate fileInfoDao) {
		this.fileInfoDao = fileInfoDao;
	}

	public void setCompanyDao(CompanyDaoHibernate companyDao) {
		this.companyDao = companyDao;
	}

	public void setDepartmentDao(DepartmentDaoHibernate departmentDao) {
		this.departmentDao = departmentDao;
	}

	@Override
	public Privilege get(String id) {
		return privilegeDao.get(id);
	}

	@Override
	public boolean save(Privilege privilege) {
		privilegeDao.save(privilege);
		return true;
	}

	@Override
	public void update(Privilege privilege) {
		privilegeDao.update(privilege);
	}

	@Override
	public void delete(String id) {
		privilegeDao.delete(id);
	}

	@Override
	public void delete(Privilege privilege) {
		privilegeDao.delete(privilege);
	}

	@Override
	public List<Privilege> findByCompany(String companyId) {
		return privilegeDao.findByCompany(companyId);
	}

	@Override
	public List<Privilege> findByFileInfo(String fileInfoId) {
		return privilegeDao.findByFileInfo(fileInfoId);
	}

	@Override
	public ListPage getAllPrivileges(int start,int limit) {
		return privilegeDao.getAllPrivileges(start,limit);
	}

	//获得完整权限信息列表
	@Override
	public ListPage getAllFullPrivileges(int start,int limit) {
		ListPage lp=privilegeDao.getAllPrivileges(start,limit);
		List<Privilege> plist=lp.list;
		List<FullPrivilegeVO> fpvolist=new ArrayList<FullPrivilegeVO>();
		for(Privilege p : plist){
			FullPrivilegeVO fpvo=getFullPrivilege(p);
			fpvolist.add(fpvo);
		}
		lp.list=fpvolist;
		return lp;
	}
	
	//获得vo
	private FullPrivilegeVO getFullPrivilege(Privilege privilege){
		Fileinfo fileInfo=fileInfoDao.get(privilege.getFileInfoId());
		Company company=companyDao.get(privilege.getCompany());
		FullPrivilegeVO fpvo=new FullPrivilegeVO();
		fpvo.setId(privilege.getId());
		fpvo.setCompany(company.getName());
		fpvo.setDelete(privilege.getDeleteop());
		
		fpvo.setDepartment(departmentDao.findById(fileInfo.getDepartment()).getCnName());
		fpvo.setModify(privilege.getModifyop());
		fpvo.setName(fileInfo.getName());
		fpvo.setProject(fileInfo.getProject());
		fpvo.setRead(privilege.getReadop());
		fpvo.setType(fileInfo.getType());
		fpvo.setVersion(fileInfo.getVersion());
		return fpvo;
	}

	//搜索权限
	@Override
	public ListPage searchFullPrivilege(String fileName,
			String project, String version, Integer type, String department,
			String company, String owner,int start,int limit) {
		// TODO Auto-generated method stub	
		ListPage lp=privilegeDao.searchPrivilege(fileName, project, version, type, department, company, owner,start,limit);
		List<Privilege> plist = lp.list;
		List<FullPrivilegeVO> flist=new ArrayList<FullPrivilegeVO>();
		for(Privilege p : plist){
			flist.add(getFullPrivilege(p));
		}
		lp.list=flist;
		return lp;
	}

	//
	@Override
	public Privilege findByOp(String fileInfoId, String companyId) {
		// TODO Auto-generated method stub
		return privilegeDao.findByOp(fileInfoId, companyId);
	}

	@Override
	public void addPrivilegeForCompany(String companyId) {
		// TODO Auto-generated method stub
		List<Fileinfo> flist=fileInfoDao.getAllFileInfos();
		for(Fileinfo fi : flist){
			Privilege p=new Privilege();
			String id=java.util.UUID.randomUUID().toString();
			p.setId(id);
			p.setCompany(companyId);
			p.setFileInfoId(fi.getId());
			p.setDeleteop(0);
			p.setModifyop(0);
			p.setReadop(0);
			privilegeDao.save(p);
		}
	}

	@Override
	public boolean checkPrivilege(String fileId, String company, int userType, String op) {
		// TODO Auto-generated method stub
		boolean ok=false;
		Fileinfo fi=fileInfoDao.get(fileId);
		if(userType==2){
			Privilege p=privilegeDao.findByOp(fileId, company);
			if(p!=null){
				if(op.equalsIgnoreCase("read")){
					ok=p.getReadop()==1;
				}else if(op.equalsIgnoreCase("delete")){
					ok=p.getDeleteop()==1;
				}else if(op.equalsIgnoreCase("modify")){
					ok=p.getModifyop()==1;
				}
			}			
		}else if(userType==1){
			
			Privilege p=privilegeDao.findByOp(fileId, company);
			if(fi.getCompany().equals(company)){
				if(p!=null){
					ok=true;
				}
			}else{
				if(p!=null){
					if(op.equalsIgnoreCase("read")){
						ok=p.getReadop()==1;
					}else if(op.equalsIgnoreCase("delete")){
						ok=p.getDeleteop()==1;
					}else if(op.equalsIgnoreCase("modify")){
						ok=p.getModifyop()==1;
					}
				}
			}
			
		}else{
			ok=true;
		}
		return ok;
	}
}
